﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace Pongster
{
    public abstract partial class BaseGameState : GameState
    {
        protected GameRoot GameRef;

        protected ControlManager ControlManager;

        protected BaseGameState TransitionTo;

        public bool Transitioning;
        public bool TransitioningIn;

        protected ChangeType changeType;

        protected TimeSpan transitionTimer;
        protected TimeSpan transitionInterval = TimeSpan.FromSeconds(0.1f);

        public Texture2D transition;

        public Color transColor;

        public SpriteFont menuFont;

        public BaseGameState(Game game, GameStateManager manager)
            : base(game, manager)
        {
            GameRef = (GameRoot)game;
            HasCursor = true;
        }

        public override void Initialize()
        {
            TransitioningIn = true;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            ContentManager Content = Game.Content;

            menuFont = Content.Load<SpriteFont>(@"Fonts\ControlFont");
            ControlManager = new ControlManager(menuFont, GameRef);
           
            transition = Content.Load<Texture2D>(@"Backgrounds\transition");

            TransitioningIn = true;
            transColor = Color.White;

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if (this.IsPopup == false)
            {
                if (TransitioningIn == true)
                {
                    transColor.A -= 6;
                }

                if (transColor.A <= 10)
                {
                    TransitioningIn = false;
                    transColor.A = 255;
                }
            }

            if (HasCursor == true)
                Settings.isCursorVisible = true;
            else
                Settings.isCursorVisible = false;

            if (Transitioning)
            {
                transitionTimer += gameTime.ElapsedGameTime;

                if (transitionTimer >= transitionInterval)
                {
                    Transitioning = false;
                    switch (changeType)
                    {
                        case ChangeType.Change:
                            StateManager.ChangeState(TransitionTo);
                            break;
                        case ChangeType.Pop:
                            StateManager.PopState();
                            break;
                        case ChangeType.Push:
                            StateManager.PushState(TransitionTo);
                            break;
                    }
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (Transitioning == true || TransitioningIn == true)
            {
                GameRef.SpriteBatch.Draw(transition, GameRef.BaseRectangle, transColor);
            }            
        }

        public virtual void Transition(ChangeType change, BaseGameState gameState)
        {
            Transitioning = true;
            changeType = change;
            TransitionTo = gameState;
            transitionTimer = TimeSpan.Zero;
        }
    }
}
